package com.tankbattle.controller;

import com.tankbattle.model.Bullet;
import com.tankbattle.model.Direction;
import com.tankbattle.model.Tank;
import com.tankbattle.view.GamePanel;
import com.tankbattle.view.GamePanel.GameState;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 游戏键盘监听器，处理键盘输入
 */
public class GameKeyListener extends KeyAdapter {
    
    private final GamePanel gamePanel;
    private boolean upPressed, downPressed, leftPressed, rightPressed;

    public GameKeyListener(GamePanel gamePanel) {
        this.gamePanel = gamePanel;
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
        switch (gamePanel.getGameState()) {
            case MENU:
                handleMenuKeyPressed(e);
                break;
            case PLAYING:
                handlePlayingKeyPressed(e);
                break;
            case GAME_OVER:
            case WIN:
                handleGameOverKeyPressed(e);
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (gamePanel.getGameState() == GameState.PLAYING) {
            handlePlayingKeyReleased(e);
        }
    }

    private void handleMenuKeyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            gamePanel.setGameState(GameState.PLAYING);
            gamePanel.initGame(); // 开始新游戏
        }
    }

    private void handlePlayingKeyPressed(KeyEvent e) {
        Tank playerTank = gamePanel.getPlayerTank();
        if (playerTank == null || !playerTank.isActive()) return;

        switch (e.getKeyCode()) {
            case KeyEvent.VK_W:
            case KeyEvent.VK_UP:
                upPressed = true;
                playerTank.setDirection(Direction.UP);
                playerTank.setMoving(true);
                break;
            case KeyEvent.VK_S:
            case KeyEvent.VK_DOWN:
                downPressed = true;
                playerTank.setDirection(Direction.DOWN);
                playerTank.setMoving(true);
                break;
            case KeyEvent.VK_A:
            case KeyEvent.VK_LEFT:
                leftPressed = true;
                playerTank.setDirection(Direction.LEFT);
                playerTank.setMoving(true);
                break;
            case KeyEvent.VK_D:
            case KeyEvent.VK_RIGHT:
                rightPressed = true;
                playerTank.setDirection(Direction.RIGHT);
                playerTank.setMoving(true);
                break;
            case KeyEvent.VK_SPACE:
                gamePanel.addBullet(playerTank.fire());
                break;
        }
    }

    private void handlePlayingKeyReleased(KeyEvent e) {
        Tank playerTank = gamePanel.getPlayerTank();
        if (playerTank == null || !playerTank.isActive()) return;

        switch (e.getKeyCode()) {
            case KeyEvent.VK_W:
            case KeyEvent.VK_UP:
                upPressed = false;
                break;
            case KeyEvent.VK_S:
            case KeyEvent.VK_DOWN:
                downPressed = false;
                break;
            case KeyEvent.VK_A:
            case KeyEvent.VK_LEFT:
                leftPressed = false;
                break;
            case KeyEvent.VK_D:
            case KeyEvent.VK_RIGHT:
                rightPressed = false;
                break;
        }

        // 只有当所有方向键都松开时，才停止坦克移动
        if (!upPressed && !downPressed && !leftPressed && !rightPressed) {
            playerTank.setMoving(false);
        }
    }

    private void handleGameOverKeyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            gamePanel.setGameState(GameState.MENU);
        }
    }
}
